<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Article extends Admin_Controller {
    private $webmaster_config = 'config';
	private $model_config     = 'article';	
    public function __construct(){
        parent::__construct();
		$this->load->library('form_validation');
        $this->load->library('image_lib');
		$this->set_webmaster_config(); //添加后台配置文件
	}
	
	private function set_webmaster_config(){
		$model=$this->uri->segment(1);
		$this->webmaster_config=$this->config->item('webmaster');
		$this->model_config=$this->config->item($model); //article
	}

	public function info_list(){
		$group_id		  = (int)$_GET['group_id'];
		$page_name	 	  = $group_id;	
		$query_string     = query_string('page');
    	$webmaster_config = $this->webmaster_config;
		$model_config 	  = $this->model_config;
		$list=$this->data->get_all('ca_article',"group_id='$group_id'",'*','my_order desc,aid desc');
		$data=array(
			'group_id'				=> $group_id,
            'page_name'				=> $page_name,
            'model_config'  		=> $model_config,
            'webmaster_config'		=> $webmaster_config,
            'list'					=> $list,
            'query_string'			=> $query_string
        );
		$this->load->view(THEME_PATH.'/article.php',$data);
	}
	
	public function info_mod(){
		$query_string     = query_string('page');
		$aid			  = (int)$_GET['aid'];
		$item			  = $this->data->get_one('ca_article',"aid='$aid'");
		$group_id		  = $item['group_id']?$item['group_id']:$_GET['group_id'];
		$page_name	 	  = $group_id;	
    	$webmaster_config = $this->webmaster_config;
		$model_config 	  = $this->model_config;
		$data=array(
			'group_id'				=> $group_id,
            'page_name'				=> $page_name,
            'model_config'  		=> $model_config,
            'webmaster_config'		=> $webmaster_config,
            'item'					=> $item,
            'aid'					=> $aid,
            'query_string'			=> $query_string
        );
		$this->load->view(THEME_PATH.'/article.php',$data);
	}
	
	public function info_mod_action(){
		$query_string     =	  $this->input->post('query_string',true);
		$webmaster_config =   $this->webmaster_config;
		$model_config 	  =   $this->model_config;
		$group_id		  =   $this->input->post('group_id',true);
        $title 		   	  =   $this->input->post('title',true);
        $aid     		  =   $this->input->post('aid',true);
        $picpath_ary	  =   $this->input->post('mul_upload',true);
        $item			  =   $this->data->get_one('ca_article',"aid='$aid'");
		$contents		  =	  $_POST['contents'];
        $this->form_validation->set_rules('title', '信息标题', 'required');
        !$this->form_validation->run() && e_json(validation_errors(),0);
        //upload and resize start
        $ret_ary     = array();
		for($i=0;$i<$model_config['article_upload_num'];$i++){
			if($item['picpath_'.$i]!=$picpath_ary[$i]){
				$new_file=@upload($picpath_ary[$i],'article',$webmaster_config);
				if($model_config['article_thumb_size']) { @resize($new_file,$webmaster_config,$model_config['article_thumb_size']); }
				$ret_ary['picpath_'.$i]=$new_file;
			} else {
				$ret_ary['picpath_'.$i]=$picpath_ary[$i];
			}
		}
		//upload and resize end
		$array=array(
			'title'		=> $title,
			'group_id'	=> $group_id,
			'contents'	=> $contents
		);
		$array				= array_merge($array,(array)$ret_ary);
		if($aid){
			$this->data->update('ca_article',"aid='$aid'",$array);
		} else {
			$array['acctime'] 	= time();
			$this->data->insert('ca_article',$array);
		}
		e_json($group_id,'1');	
	}
	public function list_order(){
		$list_id		= $this->input->post('list_id',true);
		$list_val		= $this->input->post('list_val',true);
		$db_name		= $this->input->post('db_name',true);
		$list_id_ary 	= explode(',', $list_id);
		$list_val_ary	= explode(',', $list_val);
		for($i=0;$i<count($list_id_ary);$i++){
			$this->data->update('ca_'.$db_name,"aid='{$list_id_ary[$i]}'",array('my_order'=>$list_val_ary[$i]));
		}
		e_json('',1);	
	}
	public function list_del_action(){
		$query_string     = $_GET['query_string'];
		$info_id    	  = (int)$this->input->post('id',true);
		$this->info_del_action($info_id);
        e_json($query_string,1);
	}
	private function info_del_action($id){
		$webmaster_config = $this->webmaster_config;
		$model_config 	  = $this->model_config;
        $root        	  = $webmaster_config['root'];
		$where      	  = "aid='$id'";
		for($i=0;$i<$model_config['article_upload_num'];$i++){
        	del_img('ca_article',$this->data,$model_config['article_thumb_size'],$where,"picpath_".$i,$root);	
        }
        $this->data->delete('ca_article',$where);
	}

	public function group_del(){
		$group_id	= $this->input->post('group_id',true);
		$ary		= explode('|', $group_id);
		foreach((array)$ary as $k=>$v){
			$this->data->delete('ca_article',"aid='$v'");
		}
		e_json($group_id,1);
	}

	public function list_img_del(){
    	$webmaster_config = $this->webmaster_config;
		$model_config 	  = $this->model_config;
        $root        	  = $webmaster_config['root'];
		$num			  = (int)$this->input->post('num',true);
        $aid	     	  = (int)$this->input->post('id',true);
        $where       	  = "aid='$aid'";
        del_img('ca_article',$this->data,$model_config['article_thumb_size'],$where,'picpath_'.$num,$root);
        e_json('',1);
    }	
}
?>